Imports System
Imports System.Collections.Generic
Imports NUnit.Framework
Imports ValidationFramework


Namespace PropertyValidationManagerExamples
    Public Class CustomClassExample
#Region "Example"
        Public Class Person

            Private _age As Integer
            Private propertyValidationManager As MemberValidationManager

            Public Sub New()
                propertyValidationManager = ValidationManagerFactory.GetPropertyValidationManager(Me)
            End Sub

            ''' <summary>
            ''' Gets a <see lanword="bool"/> indicating if the current state is valid.
            ''' </summary>
            Public Function Validate() As Boolean
                propertyValidationManager.ValidateAll()
                Return propertyValidationManager.IsValid
            End Function


            ' Properties
            <RequiredIntRule()> _
            Public Property Age() As Integer
                Get
                    Return _age
                End Get
                Set(ByVal value As Integer)
                    _age = value
                End Set
            End Property

            ''' <summary>
            ''' Gets a <see see="ICollection"/> of <see langword="string"/>s that contain all 
            ''' the <see cref="ValidationResult.ErrorMessage"/>s for all the 
            ''' <see cref="ValidationResult"/>s in <see cref="ValidatorResultsInError"/>.
            ''' </summary>
            Public Overridable ReadOnly Property ErrorMessages() As IList(Of String)
                Get
                    Return ResultFormatter.GetErrorMessages( _
                                                            propertyValidationManager.ValidatorResultsInError)
                End Get
            End Property

            ''' <summary>
            ''' Gets <see cref="IList(Of T)"/> containing all <see cref="ValidationResult"/> for 
            ''' <see cref="Rule"/>s that have failed validation.
            ''' </summary>
            Public Overridable ReadOnly Property ValidatorResultsInError() _
                As IList(Of ValidationResult)
                Get
                    Return propertyValidationManager.ValidatorResultsInError
                End Get
            End Property


        End Class


        <TestFixture()> _
        Public Class ExampleUsage
            ' Methods
            <Test()> _
            Public Sub RunIsValid()
                Dim person As New Person

                ' Initially not valid
                Assert.IsFalse(person.Validate)

                ' Set Age
                person.Age = 10

                ' It is now valid
                Assert.IsTrue(person.Validate)
            End Sub

            <Test()> _
            Public Sub RunValidatorResultsInError()
                Dim person As New Person
                Assert.IsFalse(person.Validate)
                ' Initially there will be 1 item in ValidatorResultsInError.
                Assert.AreEqual(1, person.ValidatorResultsInError.Count)

                ' Set Age
                person.Age = 10

                ' Since Validate has not been called the count will not have changed
                Assert.AreEqual(1, person.ValidatorResultsInError.Count)
                Assert.IsTrue(person.Validate)
                ' Since Validate has now been called the count will have changed
                Assert.AreEqual(0, person.ValidatorResultsInError.Count)
            End Sub

        End Class
#End Region
    End Class
End Namespace